/*
 * @Author: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @Date: 2024-05-17 11:47:51
 * @LastEditors: thats-all-right-ha-ha 8280209+thats-all-right-ha-ha@user.noreply.gitee.com
 * @LastEditTime: 2024-05-17 18:54:44
 * @FilePath: \cq-serve\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

// var indexRouter = require("./routes/index");
// var usersRouter = require("./routes/users");
//跨域
const cors = require("cors");
//导入数据库启动文件
const { mongoose } = require("./utils/db.js");
//导入路由文件夹
const router = require("./routes/index.js");
//导入banner
// const indexBanners = require("./routes/banner.js");
var app = express();
//跨域解决
app.use(
  cors({
    origin: true, //true 设置为 req.origin.url
    methods: "GET,HEAD,PUT,PATCH,POST,DELETE", //容许跨域的请求方式
    allowedHeaders: "*", //跨域请求头
    preflightContinue: false, // 是否通过next() 传递options请求 给后续中间件
    maxAge: 1728000, //options预验结果缓存时间 20天
    credentials: true, //携带cookie跨域
    optionsSuccessStatus: 200, //options 请求返回状态码
  })
);
// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
//路由
// app.use("/", indexRouter);
// app.use("/users", usersRouter);

app.use(router);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get("env") === "development" ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render("error");
});

module.exports = app;
